//
//  TemplateFoundXGViewController.m
//  YuTang
//
//  Created by 葛悦彣 on 2024/9/26.
//

#import "TemplateFoundXGViewController.h"
#import "TemplateFoundDetailsViewController.h"
#import "TemplateFoundDetailsModel.h"
#import "TemplateManageModel.h"

@interface TemplateFoundXGViewController ()
@property (nonatomic ,strong) UILabel * mblab;
@property (nonatomic ,strong) UITextField * nameField;
@end

@implementation TemplateFoundXGViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
    // Do any additional setup after loading the view.
}
- (void)setupView{
    UIView * topview = [UIView new];
    topview.backgroundColor = KcolorHex(@"#0967FF");
    [self.view addSubview:topview];
    [topview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.offset(0);
        make.height.offset(Knav_HeightX);
    }];
    MJWeakSelf
    UIView * backview = [UIView new].addClickBlock(^{
        [weakSelf popViewController];
    });
    [topview addSubview:backview];
    [backview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.offset(44);
        make.left.bottom.offset(0);
        make.width.offset(kwt(170));
    }];
    
    UIImageView * leftimg = kimageView(@"white_path");
    [backview addSubview:leftimg];
    [leftimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(20);
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UILabel * leftlab = [DTKTool labelWithText:@"修改个人模版" andFont:kCFont(19) andTextColor:UIColor.whiteColor];
    [topview addSubview:leftlab];
    [leftlab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(43));
        make.centerY.equalTo(backview);
    }];
    
    UIView * mbview = [UIView new];
    mbview.addClickBlock(^{
        [weakSelf XZMB];
    });
    mbview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:mbview];
    [DTKTool YJ:kwt(5) andView:mbview];
    [mbview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.height.offset(kwt(53));
        make.top.equalTo(topview.mas_bottom).offset(kwt(12));
    }];
    
    UILabel * mbmslab = [UILabel new];
    mbmslab.mas_KLS().mas_KYS();
    mbmslab.attributedText = [DTKTool AttributedStringWithStr:@"*公共模版" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
    [mbview addSubview:mbmslab];
    [mbmslab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UIImageView * rightimg = kimageView(@"template_enter");
    [mbview addSubview:rightimg];
    [rightimg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-10));
        make.width.height.offset(kwt(15));
        make.centerY.offset(0);
    }];
    
    UILabel * mblab = [DTKTool labelWithText:@"请选择" andFont:kFont(18) andTextColor:KcolorHex(@"#787A79")];
    self.mblab = mblab;
    mblab.textAlignment = NSTextAlignmentRight;
    [mbview addSubview:mblab];
    [mblab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(kwt(-33));
        make.centerY.offset(0);
        make.left.equalTo(mbmslab.mas_right).offset(kwt(10));
    }];
    
    UIView * nameview = [UIView new];
    nameview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:nameview];
    [DTKTool YJ:kwt(5) andView:nameview];
    [nameview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.right.offset(kwt(-10));
        make.height.offset(kwt(53));
        make.top.equalTo(mbview.mas_bottom).offset(kwt(10));
    }];
    
    UILabel * namemslab = [UILabel new];
    namemslab.attributedText = [DTKTool AttributedStringWithStr:@"*模板名称" andNodeColor:KcolorHex(@"#222222") andSeledColor:KcolorHex(@"#FA0F0F") andFont:kCFont(17) andPattenr:@"[*]"];
    [nameview addSubview:namemslab];
    [namemslab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(kwt(10));
        make.centerY.offset(0);
    }];
    
    UITextField * nameField = [UITextField new];
    self.nameField = nameField;
    nameField.textAlignment = NSTextAlignmentRight;
    nameField.attributedPlaceholder = [DTKTool AttributedStringWithStr:@"请填写" andNodeColor:KcolorHex(@"#787A79") andSeledColor:KcolorHex(@"#787A79") andFont:kFont(18) andPattenr:@""];
    nameField.font = kFont(18);
    nameField.textColor = KcolorHex(@"#222222");
    [nameview addSubview:nameField];
    [nameField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(namemslab.mas_right).offset(kwt(10));
        make.centerY.offset(0);
        make.right.offset(kwt(-10));
        
    }];
    
    
    UIView * bottomview = [UIView new];
    bottomview.backgroundColor = UIColor.whiteColor;
    [self.view addSubview:bottomview];
    [bottomview mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.offset(0);
        make.height.offset(kwt(63) + KSafeAreaBottomHeight);
    }];
    
    UIButton * xybbtn = [UIButton buttonWithType:0];
    [DTKTool YJ:kwt(10) andView:xybbtn];
    xybbtn.addNormalText(@"下一步").addNormalColor(UIColor.whiteColor).addTarget(self,@selector(CMApi));
    xybbtn.addbackgroundColor([UIColor gradientColorWithSize:CGSizeMake(kwt(350), 1) direction:GradientColorDirectionLevel startColor:KcolorHex(@"#427FFF") endColor:KcolorHex(@"#3D5FFF")]);
    [bottomview addSubview:xybbtn];
    [xybbtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.offset(kwt(8));
        make.left.offset(kwt(12));
        make.right.offset(kwt(-12));
        make.height.offset(kwt(48));
    }];
    
    if(self.model){
        self.model = self.model;
    }
}
- (void)XZMB{
    [self.view showBusyHUD];
    [SCNetManger PostPullListWithDict:[NSMutableDictionary dictionary] andCompetionHandlerBlock:^(HTTPArrBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            NSArray * modelarray = [NSArray yy_modelArrayWithClass:TemplateManageModel.class json:model.result];
            NSMutableArray * nameArray = [NSMutableArray array];
            for (TemplateManageModel * mbmodel in modelarray) {
                [nameArray addObject:mbmodel.name];
            }
            [BRStringPickerView showPickerWithTitle:@"请选择公共模版" dataSourceArr:nameArray selectIndex:0 resultBlock:^(BRResultModel * _Nullable resultModel) {
                for (TemplateManageModel * mbmodel in modelarray) {
                    if([mbmodel.name isEqualToString:resultModel.value]){
                        if(![self.model.templateId isEqualToString: mbmodel.ID]){
                            self.model.isXG = YES;
                            self.model.templateId = mbmodel.ID;
                            self.model.templateName = mbmodel.name;
                            self.mblab.text = mbmodel.name;
                        }
                    }
                }
            }];
            
        } else {
            [self.view showWarning:model.message];
        }
    }];
}
- (void)CMApi{
    if(self.nameField.text.length < 1){
        [self.view showWarning:@"请输入模版名称"];
        return;
    }
    

    NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:@{
        @"name":self.nameField.text,
        @"id":self.model.ID,
    }];
    [self.view showBusyHUD];

    ///公共模版
    [SCNetManger PostTemplateCheckWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
        [self.view hideBusyHUD];
        if(model.code == 200){
            [self XYBBtnClick];
        } else {
            self.nameField.text = @"";
            [self.view showWarning:model.message];
        }
    }];
}
- (void)XYBBtnClick{
    
    if(self.model.templateId.length < 1){
        [self.view showWarning:@"请选择公共模版"];
        return;
    } 
    if(self.nameField.text.length < 1){
        [self.view showWarning:@"请输入模版名称"];
        return;
    }
    
    [self.view showBusyHUD];
    if(self.model.isXG){///修改了模版
        NSMutableDictionary * dict = [NSMutableDictionary dictionaryWithDictionary:@{@"id":self.model.templateId}];
        ///公共模版
        [SCNetManger PostMBDetailWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
            [self.view hideBusyHUD];
            if(model.code == 200){
                TemplateFoundDetailsModel * m = [TemplateFoundDetailsModel yy_modelWithJSON:model.result];
                for (TemplateFoundDetailsStepModelListModel * listmodel in m.stepModelList) {
                    for (TemplateFoundDetailsTaskTypeModelListModel * taskModel in listmodel.taskTypeModelList) {
                        taskModel.sourceFlag = 1;
                    }
                }
                self.model.stepModelList = m.stepModelList;
                TemplateFoundDetailsViewController * vc = [TemplateFoundDetailsViewController new];
                vc.model = m;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                [self.view showWarning:model.message];
            }
        }];
    } else {
        NSMutableDictionary * dict = [NSMutableDictionary dictionary];
        [dict setObject:self.model.ID forKey:@"id"];
        ///个人模版
        [SCNetManger PostGRMBDetailWithDict:dict andCompetionHandlerBlock:^(HTTPDicBase * model) {
            [self.view hideBusyHUD];
            if(model.code == 200){
                MJWeakSelf
                TemplateFoundDetailsModel * m = [TemplateFoundDetailsModel yy_modelWithJSON:model.result];
//                for (TemplateFoundDetailsStepModelListModel * listmodel in m.stepModelList) {
//                    for (TemplateFoundDetailsTaskTypeModelListModel * taskModel in listmodel.taskTypeModelList) {
//                        taskModel.sourceFlag = 1;
//                    }
//                }
                
                m.name = self.nameField.text;
                TemplateFoundDetailsViewController * vc = [TemplateFoundDetailsViewController new];
                vc.model = m;
                [self.navigationController pushViewController:vc animated:YES];
            } else {
                [self.view showWarning:model.message];
            }
        }];
    }
}

- (void)setModel:(TemplateFoundDetailsModel *)model{
    _model = model;
    _mblab.text = model.templateName;
    _mblab.textColor = KcolorHex(@"#222222");
    _nameField.text = model.name;
}
@end
